/**
 * 用户管理接口控制器
 */


class User {
    // 创建
    async create(ctx) {
        const { server } = ctx;
        const { account, password, role } = ctx.request.body;
        ctx.params = { account, password, role };
        const result = await server.service.user.create(ctx);
        ctx.$response({
            code: 200,
            msg: 'create success',
            result
        });
    }
    // 修改
    async update(ctx) {
        const { server } = ctx;
        const { pk, account, password, role } = ctx.request.body;
        ctx.params = { pk, account, password, role };
        const result = await server.service.user.update(ctx);
        if (result === 404) {
            ctx.$response({
                code: 404,
                msg: 'user not found'
            });
        } else {
            ctx.$response({
                code: 200,
                msg: 'update success',
                result
            });
        }
    }
    // 删除
    async delete(ctx) {
        const { server } = ctx;
        const pk = ctx.params.pk
        ctx.params = { pk }
        const result = await server.service.user.delete(ctx);
        if (result === 404) {
            ctx.$response({
                code: 404,
                msg: 'user not found'
            });
        } else if (result === 403) {
            ctx.$response({
                code: 403,
                msg: 'cannot delete'
            });
        } else {
            ctx.$response({
                code: 200,
                msg: 'delete success',
                result
            });
        }
    }
    // 查询
    async findAll(ctx) {
        const { server } = ctx;
        const result = await server.service.user.findAll(ctx);
        ctx.$response({
            code: 200,
            msg: 'find success',
            result
        });
    }
}

export default User